package org.eclipse.soc.ide4edu.assignment.wizards;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public class AssignmentDownloadWizardPage1 extends WizardPage {

	Text serverURLField;
	Combo serverNameCombo;
	Text directURLField;
	Text assignmentField;
	Text courseIDParamField;
	Button directURLChoice;
	Button customChoice;

	protected AssignmentDownloadWizardPage1(String pageName) {
		super(pageName);
		setTitle(pageName);
	}

	private void addNames(Combo serverName) {
		final String[] names = { "Moodle" };
		serverName.setItems(names);
	}

	private void checkURL(ModifyEvent e) {
		try {
			if (e.widget == serverURLField)
				new URL(serverURLField.getText());

			if (e.widget == directURLField)
				new URL(directURLField.getText());

		} catch (MalformedURLException ex) {
			updateStatus("Invalid URL");
			return;
		}
		updateStatus(null);
	}

	@Override
	public void createControl(Composite parent) {

		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout gl = new GridLayout();
		composite.setLayout(gl);

		createGeneralDetailsSection(composite);
		disableLinkGeneration();
		composite.layout();
		setControl(composite);

	}

	private void createGeneralDetailsSection(Composite composite) {

		GridLayout gl = new GridLayout();
		gl.numColumns = 3;
		GridData gd = new GridData(GridData.FILL_HORIZONTAL
				| GridData.GRAB_HORIZONTAL);

		Group general = new Group(composite, SWT.NONE);
		general.setLayout(gl);
		general.setLayoutData(gd);
		general.setText("General Info");

		gd = new GridData(GridData.FILL_HORIZONTAL);
		new Label(general, SWT.NONE).setText("Server Name: ");
		serverNameCombo = new Combo(general, SWT.READ_ONLY | SWT.DROP_DOWN);
		addNames(serverNameCombo);
		serverNameCombo.setLayoutData(gd);
		gd.horizontalSpan = 2;
		serverNameCombo.setLayoutData(gd);

		gd = new GridData(GridData.FILL_HORIZONTAL);
		new Label(general, SWT.NONE).setText("Server Address: ");
		serverURLField = new Text(general, SWT.BORDER | SWT.SINGLE);
		serverURLField.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				checkURL(e);
			}
		});
		serverURLField.setLayoutData(gd);
		gd.horizontalSpan = 2;

		gd = new GridData(GridData.FILL_HORIZONTAL);
		directURLChoice = new Button(general, SWT.RADIO);
		directURLChoice.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				disableLinkGeneration();

			}
		});
		directURLChoice.setSelection(true);

		directURLChoice.setText("I have a Direct Link for the Assignment");
		gd.horizontalSpan = 3;
		directURLChoice.setLayoutData(gd);

		new Label(general, SWT.NONE).setText("Direct Link:");
		gd = new GridData(GridData.FILL_HORIZONTAL);
		directURLField = new Text(general, SWT.BORDER | SWT.SINGLE);
		directURLField.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				checkURL(e);
			}
		});
		directURLField.setLayoutData(gd);
		gd.horizontalSpan = 2;

		gd = new GridData(GridData.FILL_HORIZONTAL);
		customChoice = new Button(general, SWT.RADIO);
		customChoice.setText("Generate Link");
		customChoice.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				disableDirectLink();
			}
		});
		customChoice.setLayoutData(gd);
		gd.horizontalSpan = 3;

		gd = new GridData(GridData.FILL_HORIZONTAL);
		new Label(general, SWT.NONE).setText("Assignment Name: ");
		assignmentField = new Text(general, SWT.BORDER | SWT.SINGLE);
		assignmentField.setLayoutData(gd);
		gd.horizontalSpan = 2;

		gd = new GridData(GridData.FILL_HORIZONTAL);
		new Label(general, SWT.NONE).setText("Course ID (URL Param)");
		courseIDParamField = new Text(general, SWT.SINGLE | SWT.BORDER);
		courseIDParamField.setLayoutData(gd);
		gd.horizontalSpan = 2;
	}

	private void disableDirectLink() {
		directURLField.setEnabled(false);

		assignmentField.setEnabled(true);
		courseIDParamField.setEnabled(true);
	}

	private void disableLinkGeneration() {

		directURLField.setEnabled(true);

		assignmentField.setEnabled(false);
		courseIDParamField.setEnabled(false);
	}

	public String getAssignmentName() {
		return assignmentField.getText();
	}

	public String getCourseID() {
		return courseIDParamField.getText();
	}

	public String getDirectURL() {
		return directURLField.getText();
	}

	public String getServerName() {
		return serverNameCombo.getText();
	}

	public String getServerURL() {
		return serverURLField.getText();
	}

	public String getUserChoice() {

		if (directURLChoice.getSelection() == true) {
			return "direct";
		}
		if (customChoice.getSelection() == true) {
			return "generate";
		}

		return null;
	}

	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}

}
